package org.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.demo.entity.InspectionLog;
import org.example.demo.mapper.InspectionLogMapper;
import org.example.demo.service.InspectionLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 考察日志服务实现类
 */
@Service
public class InspectionLogServiceImpl extends ServiceImpl<InspectionLogMapper, InspectionLog>
                implements InspectionLogService {

        @Override
        public Page<InspectionLog> selectPage(InspectionLog inspectionLog, Integer pageNum, Integer pageSize) {
                Page<InspectionLog> page = new Page<>();
                page.setCurrent(pageNum);
                page.setSize(pageSize);

                QueryWrapper<InspectionLog> queryWrapper = new QueryWrapper<>();

                // 构建查询条件
                queryWrapper.lambda()
                                .eq(inspectionLog.getUserId() != null, InspectionLog::getUserId,
                                                inspectionLog.getUserId())
                                .eq(inspectionLog.getStatus() != null, InspectionLog::getStatus,
                                                inspectionLog.getStatus())
                                .eq(inspectionLog.getConclusion() != null, InspectionLog::getConclusion,
                                                inspectionLog.getConclusion())
                                .like(inspectionLog.getChecker() != null && !inspectionLog.getChecker().isEmpty(),
                                                InspectionLog::getChecker, inspectionLog.getChecker())
                                .eq(inspectionLog.getInspectionScore() != null,
                                                InspectionLog::getInspectionScore, inspectionLog.getInspectionScore())
                                .ge(inspectionLog.getRecordDate() != null, InspectionLog::getRecordDate,
                                                inspectionLog.getRecordDate())
                                .orderByDesc(InspectionLog::getRecordDate);

                return this.page(page, queryWrapper);
        }

        @Override
        public boolean save(InspectionLog entity) {
                LocalDateTime now = LocalDateTime.now();
                entity.setCreateTime(now);
                entity.setUpdateTime(now);
                return super.save(entity);
        }

        @Override
        public boolean updateById(InspectionLog entity) {
                entity.setUpdateTime(LocalDateTime.now());
                return super.updateById(entity);
        }
}